package com.apps.ftapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.apps.ftapp.R;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.DensityUtils;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.SharedPreferencesUtil;
import com.apps.ftapp.util.StatusBarUtils;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

public class UpdPasswordActivity extends AppCompatActivity {

    @BindView(R.id.old_pwd)
    EditText oldPwd;
    @BindView(R.id.pwd)
    EditText pwd;
    @BindView(R.id.repassword)
    EditText repassword;
    @BindView(R.id.tijiao)
    TextView tijiao;
    @BindView(R.id.backBtn)
    ImageView back_btn;

    private Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upd_password);
        ButterKnife.bind(this);


        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String oldpwd = oldPwd.getText().toString();
                String password = pwd.getText().toString();
                String repwd = repassword.getText().toString();
                if ("".equals(oldPwd)) {
                    Toasty.normal(getApplicationContext(), "请输入原密码").show();
                    return;
                }
                if ("".equals(password)) {
                    Toasty.normal(getApplicationContext(), "请输入新密码").show();
                    return;
                }
                if ("".equals(repwd)) {
                    Toasty.normal(getApplicationContext(), "请输入确认密码").show();
                    return;
                }

                if (!password.equals(repwd)) {
                    Toasty.normal(getApplicationContext(), "两次密码输入不一致").show();
                    return;
                }

                //请求网络
                loadData(oldpwd, password, repwd);
            }


        });

        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        StatusBarUtil.setTranslucentForImageViewInFragment(UpdPasswordActivity.this, 0, null);
        StatusBarUtils.StatusBarLightMode(this);
        init();
    }

    private void init() {
        getAndroiodScreenProperty();
        ViewGroup.LayoutParams params = tijiao.getLayoutParams();
        params.height = (int) (((DensityUtils.dp2px(this, (Constant.SCREEN_WIDTH - 120)) / 231.00)) * 38);
        tijiao.setLayoutParams(params);
    }


    public void loadData(final String oldpwd, final String password, String repwd) {
        RequestParams params = new RequestParams();
        params.add("ypassword", oldpwd);
        params.add("password", password);
        params.add("confirmPassword", repwd);
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("password/reset/changepass", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    //请求成功
                    Map jsonBean = gson.fromJson(new String(responseBody), Map.class);
                    L.i("main", jsonBean.toString());
                    if (1 == Tools.isDoubleNull(jsonBean.get("success")) || 1.0 == Tools.isDoubleNull(jsonBean.get("success"))) {
                        Toasty.normal(getApplicationContext(), "密码设置成功").show();

                        tuichu();

                    } else {
                        Toasty.normal(getApplicationContext(), Tools.isNull(jsonBean.get("error"))).show();
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }


    public void getAndroiodScreenProperty() {
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;// 屏幕宽度（像素）
        int height = dm.heightPixels; // 屏幕高度（像素）
        float density = dm.density;//屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;//屏幕密度dpi（120 / 160 / 240）
        //屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        int screenWidth = (int) (width / density);//屏幕宽度(dp)
        Constant.SCREEN_WIDTH = screenWidth;
        //int screenHeight = (int) (height / density);//屏幕高度(dp)
    }

    private void tuichu() {

        SharedPreferencesUtil.remove(getApplicationContext(), "uid");
        SharedPreferencesUtil.remove(getApplicationContext(), "token");
        //  Log.i("MAIN",jsonBean.toString());
        SharedPreferencesUtil.putBoolean(UpdPasswordActivity.this, SharedPreferencesUtil.IS_LOGIN, true);

        Intent intent = new Intent();
        intent.setClass(UpdPasswordActivity.this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }


}
